package uk.ac.aber.paws.server.db;

import uk.ac.aber.paws.client.db.DBCall;
import uk.ac.aber.paws.server.config.ConfigManager;
import uk.ac.aber.paws.server.config.Constants;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 16th August 2011
 * 
 * DBLocalCallQueue.java
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class DBLocalCallQueue extends ParentDBOps {

	/**
	 * Makes an immediate call to the database when invokes, i.e. it is not queued.
	 * @param dbCall
	 */
	public void makeImmediateDBCall(DBCall dbCall) {
	
		if (conn == null) {
			connectDB(new DBConnectionObject(Constants.SQLSERVERDB,ConfigManager.getProperty("app.db.principal"),ConfigManager.getProperty("app.db.mirror"),ConfigManager.getProperty("app.db.name"),ConfigManager.getProperty("app.db.username"),ConfigManager.getProperty("app.db.password"),Integer.parseInt(ConfigManager.getProperty("app.db.sqlserverport"))));
		}
		
		try {
			
			// check if the database connection is still valid
			if (!conn.isValid(0)) {
				
				// if not....reopen it
				connectDB(new DBConnectionObject(Constants.SQLSERVERDB,ConfigManager.getProperty("app.db.principal"),ConfigManager.getProperty("app.db.mirror"),ConfigManager.getProperty("app.db.name"),ConfigManager.getProperty("app.db.username"),ConfigManager.getProperty("app.db.password"),Integer.parseInt(ConfigManager.getProperty("app.db.sqlserverport"))));

			}
			else {
				
				DBCallProcessor.executeCall(conn,dbCall);
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
